package cn.ml.mybatisdemo.servlet;

import cn.ml.mybatisdemo.dao.PersonDao;
import cn.ml.mybatisdemo.entity.Person;
import org.apache.ibatis.session.SqlSession;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

public class ListPersonServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        PersonDao personDao = ((SqlSession)req.getAttribute("sqlSession")).getMapper(PersonDao.class);
        List<Person> persons = personDao.list();
        req.setAttribute("persons",persons);
        req.getRequestDispatcher("/WEB-INF/list.jsp").forward(req,resp);
    }
}
